// 区县管理页面
<template>
  <div class="app-container">
    <div class="cbox">
      <el-form
        :model="compForm"
        label-width="11rem"
        ref="compForm"
        size="mini"
        :rules="compFormRules"
      >
        <el-row>
          <el-col :span="4">
            <el-form-item label="省中文名称：" prop="shengName">
                <el-select  filterable allow-create clearable  v-model="compForm.shengName" @change="change">
                  <el-option
                    v-for="item in shengxl"
                    :key="item.PRV_CODE"
                    :value="item.PRV_CODE"
                    :label="item.PRV_CHNNAME"
                  ></el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="市中文名称：" prop="shiName">
                <el-select  filterable allow-create clearable v-model="compForm.shiName" >
                  <el-option
                    v-for="item in shixlA"
                    :key="item.CTYE_CODE"
                    :value="item.CTYE_CODE"
                    :label="item.CTYE_CHNNAME"
                  ></el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="区县代码：" prop="code">
              <el-input clearable maxlength="16"  v-model="compForm.code"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="区县中文名称：" prop="qxName">
              <el-input clearable maxlength="16" v-model="compForm.qxName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label-width="2rem">
              <el-button type="primary" @click="search">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-row :gutter="10" class="btn-area">
      <el-col :span="24">
        <!-- <div v-for="(item,index) in grandson" :key='index'>
          <el-button v-if="item.MDL_CODE === 'add'" size="mini" type="primary" plain @click="add">新增区县</el-button>
          <el-button v-if="item.MDL_CODE === 'edit'" size="mini" type="primary" plain @click="edit">编辑区县</el-button>
          <el-button v-if="item.MDL_CODE === 'delete'" size="mini" type="primary" plain @click="del">删除区县</el-button>
        </div>-->
        <el-button size="mini" type="success" plain @click="add">新增区县</el-button>
        <el-button size="mini" type="primary" plain @click="edit">编辑区县</el-button>
        <el-button size="mini" type="danger" plain @click="del">删除区县</el-button>
      </el-col>
    </el-row>
    <div class="table">
      <div class="inner" ref="inner">
        <el-table
          :data="compList"
          :highlight-current-row="true"
          @row-click="getCurrentRow"
          @row-dblclick="storageDetail"
          border
          :header-cell-style="{
            textAlign: 'center'
          }"
          :height="maxHeight"
        >
          <el-table-column align="center" label="序号" type="index" :index="indexMethod" show-overflow-tooltip  width="60"></el-table-column>
          <el-table-column size="mini" align="left" label="区县代码" prop="ctyCode" show-overflow-tooltip></el-table-column>
          <el-table-column size="mini" align="left" label="省中文名称" prop="prvChnName" show-overflow-tooltip></el-table-column>
          <el-table-column size="mini" align="left" label="市中文名称" prop="ctyCityName" show-overflow-tooltip></el-table-column>
          <el-table-column size="mini" align="left" label="区县中文名称" prop="ctyChnname" show-overflow-tooltip></el-table-column>
          <el-table-column size="mini" align="left" label="英文名称" prop="ctyEgsname" show-overflow-tooltip></el-table-column>
          <el-table-column size="mini" align="left" label="创建员工" prop="ctyInsertuser" show-overflow-tooltip></el-table-column>
          <el-table-column size="mini" align="center" label="创建时间" prop="ctyInsertdt" show-overflow-tooltip></el-table-column>
          <el-table-column size="mini" align="left" label="最后输入员工" prop="ctyLstupduser" show-overflow-tooltip></el-table-column>
          <el-table-column size="mini" align="center" label="最后输入时间" prop="ctyLstupddt" show-overflow-tooltip></el-table-column>
        </el-table>
      </div>
    </div>

      <el-pagination
        class="pagination"
        small
        background
        ref="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="Pagination.currentPage"
        :page-sizes="Pagination.pageSizeList"
        :page-size="Pagination.pageSize"
        :layout="Pagination.layout"
        :total="Pagination.total"
      ></el-pagination>

    <el-dialog
      v-el-drag-dialog
      ref="dialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      v-if="showDialog"
      :title="operate"
      :before-close="handleClose">
      <el-form
        :model="dialogForm"
        ref="dialogForm"
        label-width="13rem"
        size="mini"
        :rules="dialogFormRules"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="省中文名称："  prop="ctyPrvCode">
              <el-select  v-model="dialogForm.ctyPrvCode" :disabled="disabled" clearable  filterable @change="changeA">
                <el-option
                  v-for="i in shengxl"
                  :key="i.PRV_CODE"
                  :label="i.PRV_CHNNAME"
                  :value="i.PRV_CODE"
                  :title="i.PRV_CHNNAME"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="市中文名称：" prop="ctyCityCode">
              <el-select  v-model="dialogForm.ctyCityCode" :disabled="disabled" clearable filterable >
                <el-option
                  v-for="i in shixl"
                  :key="i.CTYE_CODE"
                  :label="i.CTYE_CHNNAME"
                  :value="i.CTYE_CODE"
                  :title="i.CTYE_CHNNAME"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="区县代码：" prop="ctyCode">
              <el-input minlength="1" maxlength="6" clearable  v-model="dialogForm.ctyCode"  @change="uppercase('ctyCode')" :disabled="disabledA"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="区县中文名称：" prop="ctyChnname">
              <el-input  minlength="1"  maxlength="16" clearable :disabled="disabled" v-model="dialogForm.ctyChnname" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="英文名称：" prop="ctyEgsname">
              <el-input minlength="1"  maxlength="16" clearable :disabled="disabled" v-model="dialogForm.ctyEgsname" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
              <el-form-item prop="ctyRemark" label="备注：">
                <el-input maxlength="32" :disabled='disabled' type="textarea" v-model="dialogForm.ctyRemark" clearable></el-input>
              </el-form-item>
          </el-col>
      </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" v-if="see" @click="save" type="primary">保存</el-button>
        <el-button size="mini" @click="close('dialogForm')">关闭</el-button>

      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  districtCountyList,
  addShippingCompany,
  editShippingCompany,
  delShippingCompany,
  shengCityManagementXL,
  shiShippingCompany
} from '@/request/api'
import resizeMixin from '@/mixins/resize'
import paginationMixin from '@/mixins/pagination'

export default {
  name: 'router.districtCounty',
  mixins: [resizeMixin, paginationMixin],
  data() {
    return {
      disabledA:false,
      disabled:false,
      see:true,
      dialogFormStatus: '', // dialog 中 form 的状态 add/edit
      currentData: {},
      showDialog: false,
      maxHeight: -1,
      operate: '',
      compForm: {
        shengName:'',
        shiName:'',
        code:'',
        qxName:''
      },
      compFormRules: {
        cName: [{
          pattern: /^[\u4e00-\u9fa5]+$/,
          message: '只能输入中文汉字',
          trigger: 'change'
        }]
      },
      dialogForm: {},
      dialogFormRules: {
        ctyCityCode:[{
          required: true,
          message: '市中文名称不能为空',
          trigger: 'change'
        }],
        ctyPrvCode: [{
          required: true,
          message: '省中文名称不能为空',
          trigger: 'change'
        }],
        ctyChnname: [{
          required: true,
          message: '区县中文名称不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^[\u4e00-\u9fa5]+$/,
          message: '请输入中文',
          trigger: 'blur'
        }
        ],
        ctyEgsname: [{
          required: true,
          message: '英文名称不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^[a-zA-Z ]+$/,
          message: '请输入英文',
          trigger: 'blur'
        }
        ],
        ctyCode: [{
          required: true,
          message: '区县代码不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^[a-zA-Z0-9]+$/,
          message: '请输入英文和数字',
          trigger: 'blur'
        }
        ]
      },
      compList: [],
      shengxl:[],
      shixl:[],
      shixlA:[],
      box:'',
    }
  },
  mounted() {
    this.refreshList()
    // this.dataList()
    this.shengData()
    // this.shiDataA()
    this.change()
  },
  created() {
    this.resize = () => {
      this.maxHeight = this.$refs.inner.offsetHeight
    }
  },
  methods: {
    //弹出框 市下拉框数据
    changeA(row){
      this.box= row
      //市下拉框数据
      shiShippingCompany({ctyeCode:'',ctyeCityId:this.box}).then(res=>{
        this.shixl = res.data.FTKJWLHYPT
        // this.shixlA = res.data.FTKJWLHYPT
      })
    },
    //页面市下拉框数据
    change(row){
      this.box= row
      //市下拉框数据
      shiShippingCompany({ctyeCode:'',ctyeCityId:this.box}).then(res=>{
        // this.shixl = res.data.FTKJWLHYPT
        this.shixlA = res.data.FTKJWLHYPT
      })
    },
    //省下拉框
    shengData(){
      shengCityManagementXL().then(res=>{
        this.shengxl = res.data.FTKJWLHYPT
      })
    },
    uppercase(key) {
      this.dialogForm[key] = this.dialogForm[key].toUpperCase()
    },
    trimECode() {
      this.dialogForm.ctyEgsname = this.dialogForm.ctyEgsname.trim()
    },

    // 点击查询
    search() {
      // this.searchList()
      this.refreshList()
    },

    //获取列表数据
    getList(currentPage, pageSize, updateTotal) {
      districtCountyList({
        ctyCode:this.compForm.code,
        ctyChnname:this.compForm.qxName,
        ctyCityCode:this.compForm.shiName,
        ctyPrvCode:this.compForm.shengName,
        PageIndexs: this.Pagination.currentPage.toString(),
        PageSizes: this.Pagination.pageSize.toString()
      }).then(response => {
        this.compList = response.data
        updateTotal(Number(response.total))
      })
    },
    // 新增
    add() {
      this.showDialog = true
      this.operate = '新增区县信息'
      this.shixl= []
      this.see = true
      this.disabled = false
      this.disabledA = false
      this.dialogFormStatus = 'add'
      if (this.$refs['dialogForm']) {
        this.$refs['dialogForm'].resetFields()
      }
      this.resetDialogForm()
    },
    //编辑
    edit() {
      this.dialogFormStatus = 'edit'
      if (Object.keys(this.currentData).length == 0) {
        this.$alert('请选择需要编辑的数据！', '提示', {
          confirmButtonText: '确定'
        })
        return
      }

      this.showDialog = true
      this.operate = '编辑区县信息'
      this.see = true
      this.disabled = false
      this.disabledA=true,
      this.dialogForm = Object.assign({}, this.currentData)
      new Promise((resolve) => {
        resolve('')
      }).then(() => {
        // this.getCountryList()
      })
        // .then(() => {
        //   this.getChanges()
        // })
        .then(() => {
          this.$refs.dialogForm.clearValidate()
        })
    },
    //删除
    del() {
      if (Object.keys(this.currentData).length == 0) {
        this.$alert('请选择需要删除的数据', '提示', {
          confirmButtonText: '确定'
        })
        return
      }

      this.$confirm('您确认要删除此数据吗?', '提示', {
        closeOnClickModal: false
      }).then(() => {
        delShippingCompany({ctyCode:this.currentData.ctyCode}).then(() => {
          this.$message.success({
            message: '删除成功！',
            showClose: true
          })
          this.refreshList()
        })
      })
        .catch(() => {})
    },
    save() {
      // this.trimECode()
      if (this.dialogFormStatus === 'add') {
        this.commitAdd('dialogForm')
      } else if (this.dialogFormStatus === 'edit') {
        this.commitEdit('dialogForm')
      }
    },
    commitAdd(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addShippingCompany(this.dialogForm).then(() => {
            this.$message.success({
              message: '新增数据成功！',
              showClose: true
            })
            this.showDialog = false
            this.refreshList()
          })
        }
      })
    },
    commitEdit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          editShippingCompany(this.dialogForm).then(() => {
            this.$message.success({
              message: '编辑数据成功！',
              showClose: true
            })
            this.showDialog = false
            this.refreshList()
          })
        }
      })
    },
    close(formName) {
      this.$refs[formName].resetFields()
      this.showDialog = false
    },
    resetDialogForm() {
      this.dialogForm = {
        ctyPrvCode:'',
        // prvPrvCode:'', //省中文名称
        ctyCityCode:'', //市中文名称
        ctyChnname:'', // 区县中文名称
        ctyCode:'', //区县代码
        ctyEgsname:'', //英文名称
        ctyRemark:'',
        ctyInsertuser:'',

      }
    },
    // 获取当前选中行
    getCurrentRow(row) {
      this.currentData = row
    },
    //双击查看
    storageDetail() {
      this.showDialog = true
      this.operate = '查看区县信息'
      this.see = false
      this.disabled = true
      this.disabledA = true
      this.dialogForm = Object.assign({}, this.currentData)
    },
    handleClose(done) {
      if (this.$refs.dialogForm) {
        this.$refs.dialogForm.resetFields()
        done()
      }
    },
  }
}

</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}

.app-container {
  height: 100%;
  width: 100%;
  position: relative;
  display: flex;
  flex-direction: column;

  .table {
    flex: 1;
    position: relative;
    overflow: hidden;

    .inner {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }

    .el-pagination {
      height: 4rem;
      flex: 0 0 auto;
      margin: 1rem 0;
    }
  }
}
</style>
